package org.mcjug.model;

import org.mcjug.R;
import org.mcjug.YambaApplicationInterface;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class YambaUtils {

	public static void broadcastNeedTwitterLogin(Context context) {
		context.sendBroadcast(new Intent(context
				.getString(R.string.login_to_twitter_intent_action_name)));
	}

	public static void rescheduleAlarm(Context context) {
		unscheduleAlarm(context);
		scheduleAlarm(context);
	}

	public static void unscheduleAlarm(Context context) {
		Intent updateIntent = new Intent(context, UpdateIntentService.class);
		PendingIntent pendingIntent = PendingIntent.getService(context, -1,
				updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		AlarmManager alarmManager = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		alarmManager.cancel(pendingIntent);
	}

	public static void scheduleAlarm(Context context) {
		int interval = ((YambaApplicationInterface) context.getApplicationContext())
				.getInterval();
		if (interval == YambaApplicationInterface.INTERVAL_NEVER) {
			return;
		}
		Intent updateIntent = new Intent(context, UpdateIntentService.class);
		PendingIntent pendingIntent = PendingIntent.getService(context, -1,
				updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		AlarmManager alarmManager = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE); // 5

		alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0,
				interval, pendingIntent);
	}

}
